package com.sunnyebuy.merchant.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.sunnyebuy.merchant.dao.CustomerOrderDAO;
import com.sunnyebuy.merchant.po.CustomerOrder;
import com.sunnyebuy.merchant.service.CustomerOrderService;
import com.sunnyebuy.merchant.status.PaymentStatus;
import com.sunnyebuy.merchant.status.ShipStatus;

/**
 * <p>
 * Title: CustomerOrderServiceImpl.java
 * </p>
 * 
 * <p>
 * Description: com.sunnyebuy.merchant.service.impl
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2008-2011 All rights reserved.
 * </p>
 * 
 * <p>
 * Created: 2011-12-28 上午11:21:00
 * </p>
 * 
 * @author Wenzhong Gu
 * 
 * @version 1.0
 */
@Service(value = "customerOrderService")
public class CustomerOrderServiceImpl implements CustomerOrderService {

	@Resource
	private CustomerOrderDAO customerOrderDAO;

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<CustomerOrder> findCustomerOrderByPaymentStatus(String merchantId, PaymentStatus paymentStatus) {
		try {
			return customerOrderDAO.findCustomerOrderByPaymentStatus(merchantId, paymentStatus);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
	public void addCustomerOrder(CustomerOrder customerOrder) throws Exception {
		customerOrderDAO.addCustomerOrder(customerOrder);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = Exception.class)
	public void addCustomerOrders(List<CustomerOrder> customerOrders) throws Exception {
		for (int i = 0; i < customerOrders.size(); i++) {
			customerOrderDAO.addCustomerOrder(customerOrders.get(i));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunnyebuy.merchant.service.CustomerOrderService#findLastOrderTx()
	 */
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public String getLastOrderTx() {
		try {
			return customerOrderDAO.getLastOrderTx();
		} catch (Exception e) {
			return null;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.sunnyebuy.merchant.service.CustomerOrderService#findCustomerOrderByFulfillmentStatus(java.lang.String,
	 * com.sunnyebuy.merchant.status.ShipStatus)
	 */
	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
	public List<CustomerOrder> findCustomerOrderByShipmentStatus(String merchantId, ShipStatus shipStatus) {
		try {
			return customerOrderDAO.findCustomerOrderByShipmentStatus(merchantId, shipStatus);
		} catch (Exception e) {
			return null;
		}
	}
}